package com.primb.bench.common.dialog;

import android.support.v4.app.FragmentManager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.primb.baselibs.view.RecycleViewDivider;
import com.primb.bench.R;
import com.primb.bench.project.home.entity.KeyEntity;

import java.util.ArrayList;
import java.util.List;

import me.shaohui.bottomdialog.BaseBottomDialog;


/**
 * Created by Chen on 2017/6/9.
 * 功能描述：
 */

public class SelectListDialog extends BaseBottomDialog {
    public TextView title;
    public List<KeyEntity> dataList = new ArrayList();
    private String textTitle;
    private int colorTitle;
    private BaseQuickAdapter.OnItemClickListener mListener;
    private RecyclerView recyclerView;
    private BaseQuickAdapter<KeyEntity, BaseViewHolder> adapter;
    private ProgressBar progressBar;

    public SelectListDialog() {

    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_list_condition;
    }

    @Override
    public void bindView(View v) {
        title = (TextView) v.findViewById(R.id.tv_dialog_title);
        recyclerView = (RecyclerView) v.findViewById(R.id.recycle_dialog_select);
        recyclerView.setVisibility(View.GONE);
        progressBar = (ProgressBar) v.findViewById(R.id.loading_progress);
        progressBar.setVisibility(View.VISIBLE);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new RecycleViewDivider(getContext(), LinearLayoutManager.VERTICAL));
        adapter = new BaseQuickAdapter<KeyEntity, BaseViewHolder>(R.layout.item_dialog_condition, dataList) {
            @Override
            protected void convert(BaseViewHolder helper, KeyEntity item) {
                helper.setText(R.id.tv_item_dialog, item.toString());
            }
        };
        adapter.setOnItemClickListener(mListener);
        recyclerView.setHasFixedSize(false);
        recyclerView.setAdapter(adapter);
    }

    public void setListener(BaseQuickAdapter.OnItemClickListener mListener) {
        this.mListener = mListener;
    }

    @Override
    public float getDimAmount() {
        return 0.5f;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void show(FragmentManager fragmentManager) {
        if (!this.isVisible()) {
            super.show(fragmentManager);
        }
    }

    public void setDateList(List<KeyEntity> dataList) {
        recyclerView.setVisibility(View.VISIBLE);
        progressBar.setVisibility(View.GONE);
        this.dataList.clear();
        this.dataList = dataList;
        adapter.addData(dataList);
        adapter.notifyDataSetChanged();
    }

    public List<KeyEntity> getDataList() {
        return dataList;
    }

    public void textTitle(String textTitle) {
        this.textTitle = textTitle;
    }


    public void colorTitle(int colorTitle) {
        this.colorTitle = colorTitle;
    }
}
